<?php

class Database
{
	var $persis = true;
	
	// Main connection
	protected $m_con;
	
	// Temporary Connection
	protected $t_con;
	
	var $last_err = false;
	
	var $last_que;
	
	protected $last_que_iden;
	
	protected $t_que_iden;
	
	function __construct($db)
	{
		/*
			Array Indexes and their meaning:
				0: Database Host
				1: Database Name
				2: Database User
				3: Database Password
				4: Persistent Conection
		*/
		if ($db[4] == false)
		{
			if (!($this->m_con = mysql_connect($db[0], $db[2], $db[3])))
			{
				$this->last_err = mysql_error();
				return(false);
			}
			$this->persis = false;
		}
		else
		{
			if (!($this->m_con = mysql_pconnect($db[0], $db[2], $db[3])))
			{
				$this->last_err = mysql_error();
				return(false);
			}
		}
		if ($this->last_err == false && is_resource($this->m_con) == true)
		{
			if (!mysql_select_db($db[1], $this->m_con))
			{
				$this->last_err = mysql_error();
				return(false);
			}
			return(true);
		}
		return(false);
	}
	
	function escape($str)
	{
		if ($this->m_con)
		{
			return(mysql_real_escape_string($str));
		}
		else
		{
			return($str);
		}
	}
	
	function query($query, $res = false)
	{
		if (is_resource($res) == true && get_resource_type($res) == 'mysql link')
		{
			$this->t_con = $res;
		}
		else
		{
			$this->t_con = $this->m_con;
		}
		
		if (is_resource($this->t_con) == true)
		{
			if (!($result = mysql_query($query, $this->t_con)))
			{
				$this->last_err = mysql_error();
				return(false);
			}
			$this->t_con = null;
			$this->last_que = $query;
			$this->last_que_iden = $result;
			return($result);
		}
	}
	
	function numrows($iden = false)
	{
		if ($iden != false && is_resource($iden) == true && get_resource_type($iden) == 'mysql link')
		{
			$this->t_que_res = $iden;
		}
		else
		{
			$this->t_que_iden = $this->last_que_iden;
		}
		if (!($result = mysql_num_rows($this->t_que_iden)))
		{
			$this->last_err = mysql_error();
			return(false);
		}
		$this->free($this->t_que_iden);
		return($result);
	}
	
	function affrows($iden = false)
	{
		if ($iden != false && is_resource($iden) == true && get_resource_type($iden) == 'mysql link')
		{
			$this->t_que_res = $iden;
		}
		else
		{
			$this->t_que_iden = $this->last_que_iden;
		}
		if (!($result = mysql_affected_rows($this->t_que_iden)))
		{
			$this->last_err = mysql_error();
			return(false);
		}
		$this->free($this->t_que_iden);
		return($result);
	}
	
	function frow($iden = false)
	{
		if ($iden != false && is_resource($iden) == true && get_resource_type($iden) == 'mysql link')
		{
			$this->t_que_iden = $iden;
		}
		else
		{
			$this->t_que_iden =  $this->last_que_iden;
		}
		if (!($result = mysql_fetch_assoc($this->t_que_iden)))
		{
			$this->last_err = mysql_error();
			return(false);
		}
		return($result);
	}
	
	function frows($iden = false)
	{
		if ($iden != false && is_resource($iden) == true && get_resource_type($iden) == 'mysql link')
		{
			$this->t_que_iden = $iden;
		}
		else
		{
			$this->t_que_iden =  $this->last_que_iden;
		}
		if (!($result = mysql_fetch_array($this->t_que_iden, MYSQL_ASSOC)))
		{
			$this->last_err = mysql_error();
			return(false);
		}
		return($result);
	}
	
	function mres($iden = false, $row = 0)
	{
		if ($iden != false && is_resource($iden) == true && get_resource_type($iden) == 'mysql link')
		{
			$this->t_que_iden = $iden;
		}
		else
		{
			$this->t_que_iden =  $this->last_que_iden;
		}
		if (!($result = mysql_result($this->t_que_iden, $row)))
		{
			$this->last_err = mysql_error();
			return(false);
		}
		return($result);
	}
	
	function free($iden = false)
	{
		if ($iden == false)
		{
			mysql_free_result($this->last_que_iden);
			return(true);
		}
		elseif($iden != false && is_resource($iden) == true && get_resource_type($iden) == 'mysql link')
		{
			mysql_free_result($iden);
			return(true);
		}
		else
		{
			return(false);
		}
	}
	
	function update($nfield, $nvalue, $sfield, $svalue, $table)
	{
		$query = "UPDATE $table SET `$nfield` = '$nvalue' WHERE `$sfield` = '$svalue';";
		$result = $this->query($query);
		
		if ($result)
		{
			return(true);
		}
		return(false);
		
	}
	
	function paginate($table, $key, $currpage = 0, $max_items = 50, $where = null)
	{
		if (!($total = $this->query('SELECT count(' . $key . ') FROM ' . $table . (($where != null) ? $where : null))))
		{
			$this->last_err					= mysql_error();
			return(false);
		}
		if (!($total_items = $this->mres($total)))
		{
			$this->last_err					= mysql_error();
			return(false);
		}
		$this->free($total);
		$total_pages 						= ceil($total_items / $max_items);
		
		if ($currpage < 1 || $currpage > $total_pages)
		{
			$page							= 1;
		}
		else
		{
			$page = $currpage;
		}
		
		$offset 							= ($page - 1) * $max_items;
		$offset								= ' LIMIT ' . $offset . ',' . $max_items;
		$return 							= array();
		$return['total_pages'] 				= $total_pages;
		$return['limiter'] 					= $offset;
		$return['curr_page']				= $currpage;
		return($return);
	}
	
}
